package com.copa.l121;

/**
 * @author copa
 * @createDate 2022-07-28 10:36
 * @function
 */
public class Test2 {

    public int maxProfit(int[] prices) {
        int min = Integer.MAX_VALUE;
        int result = 0;
        for (int i = 0; i < prices.length; i++) {
            if (min > prices[i]) {
                // 计算当前遍历最小的
                min = prices[i];
            }
            if (prices[i] - min > result) {
                // 计算最大利润，不能单看最小买入值，因为要跟后面联系起来
                result = prices[i] - min;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        //int[] prices = {7,1,5,3,6,4};
        int[] prices = {7,6,3,4,1};
        System.out.println(new Test2().maxProfit(prices));
    }
}
